layui.use(["app", "api"], function () {
    let app = layui.app
    let api = layui.api

    // 监听数据
    let data = app.proxy({
        // 分页
        paging: {
            page: 1,
            limit: 6,
            count: []
        },
        // 数据
        hot: []
    });

    /**
     * 根据数字创建数组
     * @param {number} num 数字
     * @returns array
     */
    function createArray(num) {
        let arr = [];
        for (let i = 0; i < num; i++) {
            arr.push(i);
        }
        return arr;
    }
    
    /**
     * api 渲染
     */
    function render() {
        api.post(
            "/app/kb_blog/api/project/search",
            data.paging
        ).then(function (res) {
            console.log(res)
            data.paging.count = createArray(Math.ceil(res.data.count / data.paging.limit))
            data.hot = res.data.data
        })
    }

    /**
     * 挂载
     */
    app.onMount = function () {
        render()
    }

    /**
     * 更新后
     * @param {object} bind jq绑定
     */
    app.afterUpdate = function (bind) {
        bind.find(".layui-page a").on("click", function () {
            data.paging.page = layui.$(this).html()
            render()
        })
    }
})
